//
//  BeautySView.swift
//  TestModelActor
//
//  Created by rolodestar on 2025/6/18.
//

import SwiftUI
import SwiftData
import RoNetApiLibrary

struct BeautySView: View {
    @Environment(\.modelContext) var modelContext
//    var actor: BeautyModelActor = BeautyModelActor.shared
    @EnvironmentObject var actor: BeautyModelActor
    @EnvironmentObject var store: Store
    @Query var beautys:[BeautyModel]
    @State var selectedBeauty: BeautyModel? = nil
    var body: some View {
#if os(macOS)
        NavigationSplitView {
            List{
                ForEach(beautys) {beauty in
                    VStack{
                        Text("\(beauty.id.uuidString)")
                            .lineLimit(2)
                            .foregroundStyle(beauty.localUrl != nil ? .primary : .secondary)
                        switch beauty.status{
                        case .downloading(let progress):
                            ProgressView("下载进度\(progress * 100)%", value: progress, total: 1)
                                .tint(.green)
                        default:
                            EmptyView()
                        }
                        
                    }
                    .onTapGesture {
                        selectedBeauty = beauty
                    }
                }
            }
            .toolbar {
                ToolbarItemGroup {
                    
                    Button(action: appendAndDownloadNewModel) {
                        Label("新增并下载", systemImage: "plus")
                    }
                    if selectedBeauty != nil{
                        Button {
                            download(model: selectedBeauty!)
                        } label: {
                            Label("重新下载", systemImage: "arrow.clockwise.circle")
                        }
                        Button {
                            remove(model: selectedBeauty!)
                            selectedBeauty = nil
                        } label: {
                            Label("删除", systemImage: "trash")
                        }
                        
                    }
                }
                
                
            }
            
            
        } detail: {
            if selectedBeauty != nil{
                Text(selectedBeauty!.localUrl?.absoluteString ?? "not downlaod")
            }else{
                Text("not selected")
            }
        }
        
        
        
#endif
        
    }
//    private func appendNewAndDownload() {
//        Task{
//            guard let model = await  appendNewModel() else{ return}
//            download(model: model)
//        }
//    }
    private func appendNewModel() async {
        
        store.appendNewBeauty()
//        guard let address = await RoNetApiLibrary.shared.fetchBeautyGirlVideoToNetAddress() else {return }
//        
//       await actor.append(from: address)
//        
//        return
        
        
    }
    private func appendAndDownloadNewModel() {
//        
//        Task{
//            guard let address = await RoNetApiLibrary.shared.fetchBeautyGirlVideoToNetAddress() else {return }
//            
//            await actor.appendAndDownload(from: address)
//        }
//        return
//
        store.appendNewBeautyAndDownload()
        
    }
    private func download(model: BeautyModel){
//        Task{
//            
//            let downloadManager = BeautyDownloadManager(actor: actor, model: model)
//            await downloadManager.startDownload()
//        }
        store.download(model: model)
    }
    private func remove(model: BeautyModel){
//        
//        Task{@MainActor in
//            await actor.remove(model: model)
//        }
        store.remove(model: model)
    }
}

#Preview {
    BeautySView()
}
